var express = require('express')
var router = express.Router()
var jwt = require('jwt-simple')
var models=require('../../models/models')
var auth = require('../../middleware/auth')
var type = models.getModel('dataType');


/*
 * admin information  create  2017.6.8 
 */

router.get('/',(req, res)=>{
    models.getModel('loginmodel').getAdmininfo()
          .then(result=>res.status(200).json(result))
          .catch(err=>res.status(500).json('server error'))
})

/*
 * create users create  2017.5.18 admin  admin123 
 */

router.post('/',auth.jwt(),(req, res)=>{
    if (!req.body.username||!req.body.password)return res.status(400).json('username or password can not be undefined')
    let md5 = require('crypto').createHash('md5')
    let passwordmd5=md5.update(req.body.password).digest('hex')
    models.getModel('loginmodel').registeredUserNmame(req.body.username)
          .then(result=>{
              if (result[0].count>0)return res.status(400).json('Username registered')
              models.getModel('loginmodel').registeredUser(req.body.username,passwordmd5)
                    .then(result=>res.status(200).json('success'))
                    .catch(err=>res.status(500).json('server error'))
          })
          .catch(err=>res.status(500).json('server error'))
})

router.post('/push',(req, res)=>{
    if (req.body.push!=1&&req.body.push!=2)return res.status(400).json('push should be 1 or 2')
    if (type.isNumber(req.body.Uid))return res.status(400).json('Uid should be a number')
    models.getModel('loginmodel').updatePush(req.body.Uid,req.body.push)
          .then(result=>res.status(200).json('success'))
          .catch(err=>res.status(500).json('server error'))
})

/*
 * patch update user password create time  2017/5/19 
 */

router.patch('/:Uid',auth.jwt(),(req, res)=>{
    if (type.isNumber(req.params.Uid))return res.status(400).json('Uid should be a number')
    if (!req.body.newpassword||!req.body.oldpassword)return res.status(400).json("password shouldn't be undefined")
    let oldpassword=require('crypto').createHash('md5').update(req.body.oldpassword).digest('hex')
    let newpassword=require('crypto').createHash('md5').update(req.body.newpassword).digest('hex')
    models.getModel('loginmodel').verifyPsword(req.params.Uid,oldpassword)
          .then(result=>{
              if (result[0].count==0)return res.status(400).json('Password error')
              models.getModel('loginmodel').updatePassword(req.params.Uid,newpassword)
                    .then(result=>res.status(200).json('success'))
                    .catch(err=>res.status(500).json('server error'))
          })
          .catch(err=>res.status(500).json('server error'))
})

/*
 * delete user create time  2017/6/8 
 */
 
router.delete('/:Uid',auth.jwt(),(req, res)=>{
    if (type.isNumber(req.params.Uid))return res.status(400).json('userid should be a number')
    models.getModel('loginmodel').delUserinfo(req.params.Uid)
          .then(result=>res.status(200).json('success'))
          .catch(err=>res.status(500).json('server error'))
})


module.exports = router;
